#!/usr/bin/env python

# Copyright (C) 2009-2010 GSyC/LibreSoft, Universidad Rey Juan Carlos
# 
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see http://www.gnu.org/licenses/.
#
#  Author : Jose Gato Luis <jgato@libresoft.es>
#

from GIC.Channels.Items.PhotoItem import *
from GIC.Channels.GenericChannel import *

from social.core.api import search

class InternalLayer (GenericChannel):
    
    MANDATORY_FIELDS = ["latitude", "longitude", "radius", "category"]
    CATEGORIES = ["photo", "note", "person", "sound"]
    
    def __init__ (self):
        self.options = {}
        
    def get_info(self):
        return "lgs layer with all the nodes geolocated"
           
    def get_categories(self):
        pass
            
    def set_mandatory_fields(self, dict):
    
        for field in self.MANDATORY_FIELDS:
            if not field in dict:
                return (False, field)
            else:
                self.options[field] = dict[field]
          
        return (True, "")
    
    def set_options(self, options):
        
        success, result = self.set_mandatory_fields(options)
    
        if not success:
            return False, "\"%s\" parameter is mandatory " % (result)
        
        self.options["user"] = options["user"]
                
        return True, ""
            
    def process(self):
        
        if self.options["category"] in self.CATEGORIES:
            results = search(self.options["user"], [self.options["category"]], self.search_pattern, self.options["radius"])
            if results == False:
                return False, "Unknown error in search process"
            else:
                return True, results[self.options["category"]]
        else:
            return False, "Category not supported"
        

    